package zhishu.iceyyy.com.bxq_2500.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import zhishu.iceyyy.com.bxq_2500.R;
import zhishu.iceyyy.com.bxq_2500.adapter.Sousuo_Adapter;
import zhishu.iceyyy.com.bxq_2500.api.API;
import zhishu.iceyyy.com.bxq_2500.api.Okhttp;
import zhishu.iceyyy.com.bxq_2500.bean.Serach;
import zhishu.iceyyy.com.bxq_2500.utils.JSONUtils;

public class SouSuoActivity extends AppCompatActivity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.et_completed_search)
    EditText etCompletedSearch;
    @BindView(R.id.iv_completed_search)
    ImageView ivCompletedSearch;
    @BindView(R.id.ll_complete_searxh)
    LinearLayout llCompleteSearxh;
    @BindView(R.id.activity_seetting)
    LinearLayout activitySeetting;
    @BindView(R.id.lv_lv)
    ListView lvLv;
    Sousuo_Adapter sousuo_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sou_suo);
        ButterKnife.bind(this);
        sousuo_adapter = new Sousuo_Adapter(this);


        lvLv.setAdapter(sousuo_adapter);
        lvLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Serach.DataBean.MovicesBean item = (Serach.DataBean.MovicesBean) sousuo_adapter.getItem(position);
                startActivity(new Intent(SouSuoActivity.this, Work_Activity.class).putExtra("value", "" + item.getVod_id()));

            }
        });
    }

    @OnClick({R.id.img_back, R.id.iv_completed_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.iv_completed_search:

                sousuo_adapter.clear();
                API.search(etCompletedSearch.getText().toString(), "0", new Okhttp.Objectcallback() {
                    @Override
                    public void onsuccess(String st) {
                        Log.e("88888", "xx" + st);
//                        startActivity(new Intent(this, xx.class));
                        Serach sousuo = JSONUtils.parseJSON(st, Serach.class);
                        if (sousuo.getCode().equals("200")) {
                            if ((sousuo.getData().getMovices() != null && sousuo.getData().getMovices().size() > 0)) {
                                List<Serach.DataBean.MovicesBean> data = sousuo.getData().getMovices();
                                sousuo_adapter.addItem(data);
                            } else {
                                Toast.makeText(SouSuoActivity.this, "暂无搜索结果！", Toast.LENGTH_SHORT).show();
                            }

                        } else {
                            Toast.makeText(SouSuoActivity.this, "" + sousuo.getMessage(), Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onFalia(int code, String errst) {
                        Log.e("88888", "xx" + errst);
                        Toast.makeText(SouSuoActivity.this, "网络错误！" + errst, Toast.LENGTH_SHORT).show();
                    }
                });

                break;
        }
    }
}
